home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH19 / MULTI.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-21  |  3.0 KB  |  171 lines

  1. ; Simple program to demonstrate the use of multitasking.
  2.  
  3.  
  4.         .xlist
  5.         include     stdlib.a
  6.         includelib    stdlib.lib
  7.         .list
  8.  
  9.  
  10. dseg        segment    para public 'data'
  11.  
  12. ChildPID    word    0
  13. BackGndCnt    word    0
  14.  
  15. ; PCB for our background process:
  16.  
  17. BkgndPCB    pcb    {0,offset EndStk2, seg EndStk2}
  18.  
  19. ; Data buffer to hold an input string.
  20.  
  21. InputLine    byte    128 dup (0)
  22.  
  23. dseg        ends
  24.  
  25. cseg        segment    para public 'code'
  26.         assume    cs:cseg, ds:dseg
  27.  
  28. ; A replacement critical error handler.  This routine calls prcsquit
  29. ; if the user decides to abort the program.
  30.  
  31.  
  32. CritErrMsg    byte    cr,lf
  33.         byte    "DOS Critical Error!",cr,lf
  34.         byte    "A)bort, R)etry, I)gnore, F)ail? $"
  35.  
  36. MyInt24        proc    far
  37.         push    dx
  38.         push    ds
  39.         push    ax
  40.  
  41.         push    cs
  42.         pop    ds
  43. Int24Lp:    lea    dx, CritErrMsg
  44.         mov    ah, 9            ;DOS print string call.
  45.         int    21h
  46.  
  47.         mov    ah, 1            ;DOS read character call.
  48.         int    21h
  49.         and    al, 5Fh            ;Convert l.c. -> u.c.
  50.  
  51.         cmp    al, 'I'            ;Ignore?
  52.         jne    NotIgnore
  53.         pop    ax
  54.         mov    al, 0
  55.         jmp    Quit24
  56.  
  57. NotIgnore:    cmp    al, 'r'            ;Retry?
  58.         jne    NotRetry
  59.         pop    ax
  60.         mov    al, 1
  61.         jmp    Quit24
  62.  
  63. NotRetry:    cmp    al, 'A'            ;Abort?
  64.         jne    NotAbort
  65.         prcsquit            ;If quitting, fix INT 8.
  66.         pop    ax
  67.         mov    al, 2
  68.         jmp    Quit24
  69.  
  70. NotAbort:    cmp    al, 'F'
  71.         jne    BadChar
  72.         pop    ax
  73.         mov    al, 3
  74. Quit24:        pop    ds
  75.         pop    dx
  76.         iret
  77.  
  78. BadChar:    mov    ah, 2
  79.         mov    dl, 7            ;Bell character
  80.         jmp    Int24Lp
  81. MyInt24        endp
  82.  
  83.  
  84.  
  85. ; We will simply disable INT 23h (the break exception).
  86.  
  87. MyInt23        proc    far
  88.         iret
  89. MyInt23         endp
  90.  
  91.  
  92.  
  93. ; Okay, this is a pretty weak background process, but it does demonstrate
  94. ; how to use the Standard Library calls.
  95.  
  96. BackGround    proc
  97.         sti
  98.         mov    ax, dseg
  99.         mov    ds, ax
  100.         inc    BackGndCnt        ;Bump call Counter by one.
  101.         yield                ;Give CPU back to foregnd.
  102.         jmp    BackGround
  103. BackGround    endp
  104.  
  105.  
  106. Main        proc
  107.         mov    ax, dseg
  108.         mov    ds, ax
  109.         mov    es, ax
  110.         meminit
  111.  
  112.  
  113. ; Initialize the INT 23h and INT 24h exception handler vectors.
  114.  
  115.         mov    ax, 0
  116.         mov    es, ax
  117.         mov    word ptr es:[24h*4], offset MyInt24
  118.         mov    es:[24h*4 + 2], cs
  119.         mov    word ptr es:[23h*4], offset MyInt23
  120.         mov    es:[23h*4 + 2], cs
  121.  
  122.         prcsinit        ;Start multitasking system.
  123.  
  124.         lesi    BkgndPCB    ;Fire up a new process
  125.         fork
  126.         test    ax, ax        ;Parent's return?
  127.         je    ParentPrcs
  128.         jmp    BackGround    ;Go do backgroun stuff.
  129.  
  130. ParentPrcs:    mov    ChildPID, bx    ;Save child process ID.
  131.  
  132.         print
  133.         byte    "I am timing you while you enter a string. So type"
  134.         byte    cr,lf
  135.         byte    "quickly: ",0
  136.  
  137.         lesi    InputLine
  138.         gets
  139.  
  140.         mov    ax, ChildPID    ;Stop the child from running.
  141.         kill
  142.  
  143.         printf
  144.         byte    "While entering '%s' you took %d clock ticks"
  145.         byte    cr,lf,0
  146.         dword    InputLine, BackGndCnt
  147.  
  148.         prcsquit
  149.  
  150. Quit:        ExitPgm            ;DOS macro to quit program.
  151. Main        endp
  152.  
  153. cseg            ends
  154.  
  155. sseg        segment    para stack 'stack'
  156.  
  157. ; Here is the stack for the background process we start
  158.  
  159. stk2        byte    256 dup (?)
  160. EndStk2        word    ?
  161.  
  162. ;Here's the stack for the main program/foreground process.
  163.  
  164. stk        byte    1024 dup (?)
  165. sseg        ends
  166.  
  167. zzzzzzseg    segment    para public 'zzzzzz'
  168. LastBytes    db    16 dup (?)
  169. zzzzzzseg    ends
  170.         end    Main
  171.